/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AgregarUsua.java
 *
 * Created on 24/06/2011, 09:44:05 AM
 */
package mongolo;

import java.util.ArrayList;
import Biblioteca.*;
import Servicios.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Pablo.Montero
 */
public class AgregarUsua extends javax.swing.JFrame {

    /** Creates new form AgregarUsua */
    public AgregarUsua() {
        initComponents();
        this.setDefaultCloseOperation(javax.swing.JFrame.HIDE_ON_CLOSE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        usuaApe2jTex = new javax.swing.JTextField();
        IngresarUsuario = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        usuaApejTex = new javax.swing.JTextField();
        usuaNamejTex = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        usuaIdjTex = new javax.swing.JFormattedTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("id");

        IngresarUsuario.setText("Ingresar Usuario");
        IngresarUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                IngresarUsuarioActionPerformed(evt);
            }
        });

        jLabel3.setText("Apellido");

        jLabel4.setText("Apellido");

        jLabel2.setText("nombre");

        try {
            usuaIdjTex.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#-####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(IngresarUsuario)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(usuaApe2jTex, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(usuaApejTex, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(usuaNamejTex, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(usuaIdjTex, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)))
                .addContainerGap(156, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(usuaIdjTex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(usuaNamejTex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usuaApejTex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usuaApe2jTex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addComponent(IngresarUsuario)
                .addContainerGap(132, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    IMapeoObjetos mapo = new MapeoObjetos();

    private void IngresarUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_IngresarUsuarioActionPerformed
        boolean r = false;
        try {
            String id = this.usuaIdjTex.getText();
            String nom = this.usuaNamejTex.getText();
            String ape = this.usuaApejTex.getText();
            String ape2 = this.usuaApe2jTex.getText();
            if (id.length() == 11 && !(nom.equals("")) && !(ape.equals("")) && !(ape2.equals(""))) {
                ArrayList<Usua> LisUsua = null;
                LisUsua = mapo.getUsua(id);
                if (LisUsua != null && LisUsua.size() > 0) {
                    if (SM.cMes("El suario ya existe, desea actualizarlo?")) {
                        if(mapo.UpdateUsua(id, nom, ape, ape2))
                            JOptionPane.showMessageDialog(null, "Se actualizo el usuario");
                    }
                    else
                        JOptionPane.showMessageDialog(null, "No se hicieron cambios");
                } else {
                    if(mapo.InsertUsua(id, nom, ape, ape2))
                        JOptionPane.showMessageDialog(null, "Se Inserto el usuario");
                }
            } else {
                JOptionPane.showMessageDialog(null, "Por favor complete los campos");
            }

        } catch (Exception ex) {
        } finally {
            if (r) {                
                this.usuaIdjTex.setText("");
                this.usuaNamejTex.setText("");
                this.usuaApejTex.setText("");
                this.usuaApe2jTex.setText("");
            }
            if (!("".equals(SM.lastError))) {
                JOptionPane.showMessageDialog(null, SM.lastError);
                SM.lastError = "";
            }
            int i = 0;

        }
}//GEN-LAST:event_IngresarUsuarioActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton IngresarUsuario;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField usuaApe2jTex;
    private javax.swing.JTextField usuaApejTex;
    private javax.swing.JFormattedTextField usuaIdjTex;
    private javax.swing.JTextField usuaNamejTex;
    // End of variables declaration//GEN-END:variables
}
